package com.example.malllocationserver.controller;
import com.example.malllocationserver.entity.Comments;
import com.example.malllocationserver.entity.Store;
import com.example.malllocationserver.entity.StoreUI;
import com.example.malllocationserver.repository.StoreRepository;
import com.example.malllocationserver.utils.GetToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class storeController {
    @Autowired
    private StoreRepository storeRepository;

    private GetToken Token = new GetToken();

    @RequestMapping(value="/searchByType",method = RequestMethod.GET)
    @ResponseBody
    public List<Store> searchByType(@RequestParam("type") String type){
        List<Store> stores = null;
        stores = storeRepository.searchByType(type);
        return stores;
    }

    @RequestMapping(value="/searchByName",method = RequestMethod.GET)
    @ResponseBody
    public List<Store> searchByName(@RequestParam("storename") String storename){
        List<Store> stores = null;
        stores = storeRepository.searchByName(storename);
        return stores;
    }

    @RequestMapping(value="/addComment",method = RequestMethod.POST)
    @ResponseBody
    public boolean addComment(@RequestParam("storename") String storename,
                              @RequestParam("comments") String comment,
                              @RequestParam("userid") String userid,
                              @RequestParam("time_com") String time_com){
        java.sql.Timestamp date = Token.strToDate(time_com);
        storeRepository.addComments(storename,comment,userid,date);
        return true;
    }

    @RequestMapping(value="/getComments",method = RequestMethod.GET)
    @ResponseBody
    public List<Comments> getComments(@RequestParam("storename") String storename){
        List<Comments> comments = null;
        comments = storeRepository.getComments(storename);
        return comments;
    }

    @RequestMapping(value = "/storeUI",method = RequestMethod.GET)
    @ResponseBody
    public List<StoreUI> getStoreUI(@RequestParam("storename") String storename){
        return storeRepository.getStoreUI(storename);
    }
}
